package com.cy.aircompressor.main.ui.dataanalysis.fragment;

import android.os.Bundle;
import android.view.View;

import com.cy.aircompressor.R;
import com.cy.aircompressor.config.AirConfig;
import com.cy.aircompressor.databinding.ChildContainerFragmentBinding;
import com.cy.aircompressor.entity.select.SelectDict;
import com.cy.aircompressor.main.ui.dataanalysis.fragment.child.PressureAnalysisFragment;
import com.cy.aircompressor.main.ui.dataanalysis.fragment.child.TemperatureAnalysisFragment;
import com.cy.aircompressor.main.ui.dataanalysis.fragment.child.VibrationAnalysisFragment;
import com.cy.aircompressor.main.ui.dataanalysis.viewmodel.DataAnalysisViewModel;
import com.cy.aircompressor.main.ui.onlinemonitor.fragment.OnlineMonitorFragment;
import com.cy.aircompressor.main.view.HeaderBar;
import com.cy.aircompressor.main.view.HeaderBarTab;
import com.cy.aircompressor.main.view.pop.AirDropdownPopupWindow;
import com.net.model.base.fragment.BaseVMFragment;
import com.net.model.livedatas.LiveDataBus;

import java.util.ArrayList;
import java.util.List;

import me.yokeyword.fragmentation.SupportFragment;


/**
 * Created by YoKeyword on 16/6/3.
 * 信息总览
 */
public class DataAnalysisFragment extends BaseVMFragment<ChildContainerFragmentBinding, DataAnalysisViewModel> {

    AirDropdownPopupWindow mPopupWindow;
    int currentPosition = 0;
    public static SelectDict selectDict;

    public static DataAnalysisFragment newInstance() {
        Bundle args = new Bundle();
        DataAnalysisFragment fragment = new DataAnalysisFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected ChildContainerFragmentBinding getViewBinding() {
        return ChildContainerFragmentBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initData() {
        initView();
        initFragments();
    }

    private List<SupportFragment> fragments = new ArrayList<>();

    private void initFragments() {
        SupportFragment firstFragment = findFragment(PressureAnalysisFragment.class);
        if (firstFragment != null) {
            // 这里库已经做了Fragment恢复,所有不需要额外的处理了, 不会出现重叠问题
            // 这里我们需要拿到mFragments的引用
            fragments.add(firstFragment);
            fragments.add(findFragment(TemperatureAnalysisFragment.class));
            fragments.add(findFragment(VibrationAnalysisFragment.class));
        } else {
            fragments.add(PressureAnalysisFragment.newInstance());
            fragments.add(TemperatureAnalysisFragment.newInstance());
            fragments.add(VibrationAnalysisFragment.newInstance());
            loadMultipleRootFragment(R.id.fl_container, currentPosition,
                    fragments.get(0),
                    fragments.get(1),
                    fragments.get(2));
        }
    }

    private void initView() {
        mBinding.bottomBar
                .addItem(new HeaderBarTab(mContext, getString(R.string.nav_ylqsfx)))
                .addItem(new HeaderBarTab(mContext, getString(R.string.nav_wdqsfx)))
                .addItem(new HeaderBarTab(mContext, getString(R.string.nav_zdqsfx)));
        mBinding.bottomBar.setOnTabSelectedListener(new HeaderBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position, int prePosition) {
                currentPosition = position;
                showHideFragment(fragments.get(position), fragments.get(prePosition));
            }

            @Override
            public void onTabUnselected(int position) {
            }

            @Override
            public void onTabReselected(int position) {
            }
        });

        mBinding.airCompressorView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPopupWindow == null) { //用户可能多次点击,不必重复创建对象
                    mPopupWindow = new AirDropdownPopupWindow(mContext, mBinding.airName, mBinding.airCompressorView,AirConfig.get().compressors);
                    mPopupWindow.setDropdownCallBack(new AirDropdownPopupWindow.DropdownCallBack() {
                        @Override
                        public void select(SelectDict selectDict) {
                            OnlineMonitorFragment.selectDict = selectDict;
                            LiveDataBus.get().with(fragments.get(currentPosition).getClass().getSimpleName(), SelectDict.class).post(selectDict);
                        }
                    });
                }
                mPopupWindow.showAsDropDown(mBinding.airCompressorView);//显示PopupWindow小弹窗
            }
        });
    }

    @Override
    protected void requestNetwork() {
        selectDict = AirConfig.get().compressors.get(0);
    }


    @Override
    protected void dataChange() {

    }
}
